package com.woniu.shops.service.imp;

import com.woniu.common.CustomException.UserException;
import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Shops;
import com.woniu.shops.mapper.CommodityMapper;
import com.woniu.shops.service.CommodityService;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommodityServiceImp implements CommodityService {
    @Resource
    private CommodityMapper commodityMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public List<Flower> findll(Shops shops) {
        if(shops!=null){
            List<Flower> flowersList = (List<Flower>) redisTemplate.opsForValue().get(shops.getSid()+"");
            if(flowersList!=null){
                return flowersList;
            }else {
                List<Flower> all = commodityMapper.findAll(shops);
                if(all!=null){
                    redisTemplate.opsForValue().set(shops.getSid()+"",all);
                    return all;
                }
                throw new UserException("不好意思,您的店铺没有商品了");
            }
        }
        throw new UserException("不好意思您未登陆啊");
    }

    @Override
    public void deleteFlower(Flower flower) {
        if(flower!=null){
            Integer i = commodityMapper.deleteFlower(flower);
            if(i > 0){
                redisTemplate.delete(flower.getShops().getSid()+"");
                return ;
            }
            throw new UserException("不好意思,未知异常,删除失败");
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public void updateCommodity(Flower flower) {
        if(flower!=null){
            List<Flower> flowerByName = commodityMapper.findFlowerByName(flower);
            System.out.println(flowerByName);
            if(flowerByName == null || flowerByName.size()==0){
                Integer i = commodityMapper.updateCommodity(flower);
                if(i > 0){
                    redisTemplate.delete(flower.getShops().getSid()+"");
                    return;
                }else {
                    throw new UserException("不好意思,未知异常,修改失败");
                }
            }else {
                throw new UserException("不好意思,名称重复了");
            }
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public void addsCommodity(Flower flower) {
        if(flower!=null){
            List<Flower> flowerByName = commodityMapper.findFlowerByName(flower);
            if(flowerByName == null || flowerByName.size()==0){
                Integer i = commodityMapper.addsCommodity(flower);
                if(i > 0){
                    redisTemplate.delete(flower.getShops().getSid()+"");
                    return;
                }else {
                    throw new UserException("不好意思,未知异常,修改失败");
                }
            }else {
                throw new UserException("不好意思,名称重复了");
            }
        }
        throw new UserException("不好意思,该条数据不存在");
    }

    @Override
    public List<Flower> findll() {
        return commodityMapper.findAllFlower();
    }
}
